package com.itboye.shop.me.qianbao;

import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.itboye.shop.R;
import com.itboye.shop.adapter.ZhangHuAdapter;
import com.itboye.shop.base.BaseActivity;
import com.itboye.shop.bean.BankCodeBean;
import com.itboye.shop.net.ResultEntity;
import com.itboye.shop.presenter.YuEPresenter;
import com.itboye.shop.util.loadutil.IsUtilUid;
import com.itboye.shop.util.loadutil.MAlert;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * 绑定账户
 */
public class MyZhangHuActivity extends BaseActivity implements Observer {
    ImageView zhanghu_icon;

    ImageView zhanghu_add;
    ZhangHuAdapter adapter;
    ListView zhanghu_listView;
    private List<BankCodeBean> list;
    YuEPresenter presenter;

    String type;
    String tid;//删除的是哪一条
    private Dialog dialog;// 自定义Dialog
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_zhang_hu);

        list = new ArrayList<BankCodeBean>();
        type = getIntent().getStringExtra("type");
        presenter = new YuEPresenter(this);
        showProgressDialog("加载银行卡中...", true);
        presenter.onZhangHaoList(IsUtilUid.isUid());
        showExtraDialog();

        zhanghu_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                if (type.equals("tixian")) {//提现界面跳转过来的时候返回并带数据
                    Intent data = new Intent();
                    data.putExtra("choose", list.get(position).getAccount());
                    data.putExtra("acctivty", list.get(position).getId());
                    setResult(102, data);
                    finish();
                }
            }
        });
        //长按
        zhanghu_listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                tid = list.get(position).getId();
                dialog.show();
                return true;
            }
        });
        // 当添加银行卡成功之后
        IntentFilter filter1 = new IntentFilter("tianjia.exit");
        registerReceiver(exitBroadcastReceiver, filter1);
    }

    BroadcastReceiver exitBroadcastReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            presenter.onZhangHaoList(IsUtilUid.isUid());
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(exitBroadcastReceiver);
    }

    /**
     * 删除Dialog
     */
    private void showExtraDialog() {
        dialog = new Dialog(this, R.style.rel_dialog);
        View view = LayoutInflater.from(this).inflate(
                R.layout.dialog_delete, null);
        dialog.setContentView(view);

        TextView dialog_ok = (TextView) view.findViewById(R.id.dialog_ok);
        TextView dialog_quxiao = (TextView) view.findViewById(R.id.dialog_quxiao);
        TextView user_title = (TextView) view.findViewById(R.id.user_title);
        TextView dialog_conext = (TextView) view.findViewById(R.id.dialog_conext);
        user_title.setText("删除账户");
        dialog_conext.setText("确定删除该账户？");

        dialog.setCanceledOnTouchOutside(false);
        dialog_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                showProgressDialog("删除中,请稍后", true);
                presenter.deleteZhangHao(IsUtilUid.isUid(), tid);

            }
        });
        dialog_quxiao.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                dialog.dismiss();
            }
        });
        WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        // 屏幕宽度
        int screenWidth = display.getWidth();
        // 屏幕高度
        int screenHeight = display.getHeight();
        Window window = dialog.getWindow();

        WindowManager.LayoutParams lp = window.getAttributes();
        window.setGravity(Gravity.CENTER);
        lp.width = screenWidth * 3 / 4;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);
    }

    @Override
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.zhanghu_icon:
                finish();
                break;
            case R.id.zhanghu_add:// 添加银行卡
                intent = new Intent(this, AddBankCardActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            if (entity.getEventType() == presenter.ZhangHaoList_success) {
                list = (List<BankCodeBean>) entity.getData();
                adapter = new ZhangHuAdapter(list, MyZhangHuActivity.this);
                zhanghu_listView.setAdapter(adapter);
            }
            if (entity.getEventType() == presenter.ZhangHaoList_fail) {
                MAlert.alert(entity.getData(), Gravity.CENTER);
            }
            if (entity.getEventType() == presenter.delete_zhanghao_success) {
                MAlert.alert(entity.getData(), Gravity.CENTER);
                presenter.onZhangHaoList(IsUtilUid.isUid());
                adapter.notifyDataSetChanged();
            }
            if (entity.getEventType() == presenter.delete_zhanghao_fail) {
                MAlert.alert(entity.getData(), Gravity.CENTER);
            }
        }

        try {
            closeProgressDialog();
            dialog.dismiss();
        } catch (Exception ex) {
        }
    }

}
